<script setup>
import { ref, computed, watch } from 'vue'

// Props定义
const props = defineProps({
  // 输入值
  modelValue: {
    type: [String, Number],
    default: ''
  },
  // 输入框类型
  type: {
    type: String,
    default: 'text',
    validator: (value) => ['text', 'password', 'email', 'tel', 'number', 'search'].includes(value)
  },
  // 占位符文本
  placeholder: {
    type: String,
    default: ''
  },
  // 是否禁用
  disabled: {
    type: Boolean,
    default: false
  },
  // 是否只读
  readonly: {
    type: Boolean,
    default: false
  },
  // 是否必填
  required: {
    type: Boolean,
    default: false
  },
  // 错误提示
  error: {
    type: String,
    default: ''
  },
  // 输入框尺寸
  size: {
    type: String,
    default: 'medium',
    validator: (value) => ['small', 'medium', 'large'].includes(value)
  },
  // 前缀图标
  prefixIcon: {
    type: String,
    default: ''
  },
  // 后缀图标
  suffixIcon: {
    type: String,
    default: ''
  },
  // 最大长度
  maxlength: {
    type: Number,
    default: null
  },
  // 最小长度
  minlength: {
    type: Number,
    default: null
  },
  // 宽度
  width: {
    type: String,
    default: '100%'
  }
})

// 事件定义
const emit = defineEmits(['update:modelValue', 'focus', 'blur', 'input', 'change', 'clear'])

// 本地输入值
const localValue = ref(props.modelValue)

// 聚焦状态
const isFocused = ref(false)

// 计算CSS类名
const inputClasses = computed(() => {
  return [
    'base-input-wrapper',
    `base-input-wrapper--${props.size}`,
    {
      'base-input-wrapper--focused': isFocused.value,
      'base-input-wrapper--error': props.error,
      'base-input-wrapper--disabled': props.disabled,
      'base-input-wrapper--has-prefix': props.prefixIcon,
      'base-input-wrapper--has-suffix': props.suffixIcon
    }
  ]
})

// 计算样式
const inputStyles = computed(() => {
  return {
    width: props.width
  }
})

// 监听值变化
watch(() => props.modelValue, (newValue) => {
  localValue.value = newValue
})

// 处理输入变化
const handleInput = (event) => {
  localValue.value = event.target.value
  emit('update:modelValue', event.target.value)
  emit('input', event)
}

// 处理焦点获取
const handleFocus = (event) => {
  isFocused.value = true
  emit('focus', event)
}

// 处理焦点失去
const handleBlur = (event) => {
  isFocused.value = false
  emit('blur', event)
}

// 处理值变化
const handleChange = (event) => {
  emit('change', event)
}

// 清除输入
const handleClear = () => {
  if (!props.disabled && !props.readonly) {
    localValue.value = ''
    emit('update:modelValue', '')
    emit('clear')
  }
}

// 处理前缀图标点击
const handlePrefixClick = (event) => {
  event.stopPropagation()
  emit('prefix-click', event)
}

// 处理后缀图标点击
const handleSuffixClick = (event) => {
  event.stopPropagation()
  emit('suffix-click', event)
}
</script>

<template>
  <div :class="inputClasses" :style="inputStyles">
    <!-- 前缀图标 -->
    <span
      v-if="prefixIcon"
      class="input-prefix"
      @click="handlePrefixClick"
    >
      {{ prefixIcon }}
    </span>
    
    <!-- 输入框 -->
    <input
      v-model="localValue"
      :type="type"
      :placeholder="placeholder"
      :disabled="disabled"
      :readonly="readonly"
      :required="required"
      :maxlength="maxlength"
      :minlength="minlength"
      class="base-input"
      @input="handleInput"
      @focus="handleFocus"
      @blur="handleBlur"
      @change="handleChange"
    />
    
    <!-- 后缀图标 -->
    <span
      v-if="suffixIcon"
      class="input-suffix"
      @click="handleSuffixClick"
    >
      {{ suffixIcon }}
    </span>
    
    <!-- 清除按钮 -->
    <span
      v-else-if="localValue && !disabled && !readonly && type !== 'password'"
      class="input-clear"
      @click="handleClear"
      title="清除"
    >
      ×
    </span>
  </div>
  
  <!-- 错误提示 -->
  <div v-if="error" class="input-error-text">{{ error }}</div>
</template>

<style scoped>
.base-input-wrapper {
  position: relative;
  display: inline-block;
}

.base-input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #dcdfe6;
  border-radius: 6px;
  font-size: 14px;
  line-height: 1.5;
  color: #303133;
  background-color: #fff;
  transition: all 0.3s;
  outline: none;
  box-sizing: border-box;
}

/* 尺寸样式 */
.base-input-wrapper--small .base-input {
  padding: 6px 10px;
  font-size: 12px;
}

.base-input-wrapper--medium .base-input {
  padding: 10px 12px;
  font-size: 14px;
}

.base-input-wrapper--large .base-input {
  padding: 14px 16px;
  font-size: 16px;
}

/* 聚焦状态 */
.base-input-wrapper--focused .base-input {
  border-color: #4c6ef5;
  box-shadow: 0 0 0 2px rgba(76, 110, 245, 0.1);
}

/* 错误状态 */
.base-input-wrapper--error .base-input {
  border-color: #ff6b35;
  box-shadow: 0 0 0 2px rgba(255, 107, 53, 0.1);
}

/* 禁用状态 */
.base-input-wrapper--disabled .base-input {
  background-color: #f5f7fa;
  border-color: #e4e7ed;
  color: #c0c4cc;
  cursor: not-allowed;
}

/* 带前缀图标时的padding */
.base-input-wrapper--has-prefix .base-input {
  padding-left: 36px;
}

/* 带后缀图标时的padding */
.base-input-wrapper--has-suffix .base-input {
  padding-right: 36px;
}

/* 前缀图标 */
.input-prefix {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: #c0c4cc;
  font-size: 16px;
  pointer-events: all;
  cursor: pointer;
  transition: color 0.3s;
}

.input-prefix:hover {
  color: #4c6ef5;
}

/* 后缀图标 */
.input-suffix {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: #c0c4cc;
  font-size: 16px;
  pointer-events: all;
  cursor: pointer;
  transition: color 0.3s;
}

.input-suffix:hover {
  color: #4c6ef5;
}

/* 清除按钮 */
.input-clear {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: #c0c4cc;
  font-size: 18px;
  cursor: pointer;
  transition: color 0.3s;
  width: 16px;
  height: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
}

.input-clear:hover {
  color: #909399;
}

/* 错误提示文本 */
.input-error-text {
  font-size: 12px;
  color: #ff6b35;
  margin-top: 4px;
  line-height: 1;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .base-input {
    font-size: 14px;
  }
  
  .base-input-wrapper--large .base-input {
    padding: 12px 14px;
    font-size: 15px;
  }
}
</style>